//
//  ViewController.m
//  UI_08_homework_UITableView
//
//  Created by li cheng on 16/7/25.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "VCOfDetails.h"
#import "VCOfAddContact.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, VCOfAddContactDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfSource;
@property (nonatomic , copy) NSString *string;
@property (nonatomic, strong) NSMutableDictionary *dic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 100) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    // 指定代理人
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // 注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    // 解析plist文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    self.arrayOfSource = [NSArray arrayWithContentsOfFile:path].mutableCopy;
    // 创建 rightBarButtonItem
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
// 进入编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
// 完成编辑的时候实现方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [_arrayOfSource removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

// 协议方法,row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrayOfSource.count;
}
// 协议方法,cell的具体显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.textLabel.text = [_arrayOfSource[indexPath.row] objectForKey:@"name"];
    return cell;
}
// 协议方法,当我点击cell的时候所触发的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfDetails *vcOfDetalis = [[VCOfDetails alloc] init];
    vcOfDetalis.dic = self.arrayOfSource[indexPath.row];
    [self.navigationController pushViewController:vcOfDetalis animated:YES];
}
- (void)addAction:(UIBarButtonItem *)barButton{
    VCOfAddContact *vcOfAddContact = [[VCOfAddContact alloc] init];
    vcOfAddContact.vcOfAddContactDelegate = self;
    [self.navigationController pushViewController:vcOfAddContact animated:YES];
}
- (void)addContact:(NSMutableDictionary *)dic{
    [self.arrayOfSource addObject:dic];
    [self.tableView reloadData];
}
// 其他
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
